package com.example.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.demo.pojo.Address;
import com.example.demo.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;

@RestController
@Api(value = "收货地址",tags = "收货地址")
public class addressController {

    @Autowired
    private AddressService addressService;

    //新增收货地址
    @ApiOperation(value = "新增收货地址",notes = "新增收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id",name = "userid"),
            @ApiImplicitParam(value = "省市",name = "Provinces"),
            @ApiImplicitParam(value = "详细地址",name = "DetailedAddress"),
            @ApiImplicitParam(value = "收货人",name = "name"),
            @ApiImplicitParam(value = "联系电话",name = "ContactNumber")
    })
    @RequestMapping(value = "/insertAddress",method = RequestMethod.POST)
    public String insertAddress(int userid,String Provinces,String DetailedAddress,String name,String ContactNumber){
        Address address=new Address();
        address.setContactnumber(ContactNumber);
        address.setDetailedaddress(DetailedAddress);
        address.setName(name);
        address.setProvinces(Provinces);
        address.setUserid(userid);
        //通过用户id查询用户收货地址
        List<Address> addressList =addressService.selectByUserId(userid);
        if (addressList.size()==0){
            address.setIsdefault(1);
            int i1=addressService.insert(address);
            if (i1>0){
                return "添加成功，以设为默认";
            }else {
                return "添加失败";
            }
        }
        int i=addressService.insert(address);
        if (i>0){
            return "添加收货地址成功";
        }else {
            return "添加失败";
        }

    }

    //删除收货地址
    @ApiOperation(value = "删除收货地址",notes = "删除收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressid",value = "收货地址id"),
            @ApiImplicitParam(name = "isdefault",value = "收货地址默认标识"),
            @ApiImplicitParam(name="userid",value = "用户id")
    })
    @RequestMapping(value = "/delectAddress",method = RequestMethod.POST)
    public String delectAddress(int addressid,int isdefault,int userid){
        int i=addressService.delectByAdddress(addressid);
        if (i>0){
            if (isdefault==1){
                List<Address> addressList=addressService.selectByUserId(userid);
                Address address=addressList.get(0);
                address.setIsdefault(1);
                while (true){
                    int j=addressService.updateByAddress(address);
                    if (j>0){
                        break;
                    }
                }
            }
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    //修改收货地址
    @ApiOperation(value = "修改收货地址",notes = "修改收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "收货地址id",name = "addressId"),
            @ApiImplicitParam(value = "省市",name = "Provinces"),
            @ApiImplicitParam(value = "详细地址",name = "DetailedAddress"),
            @ApiImplicitParam(value = "联系电话",name = "ContactNumber"),
            @ApiImplicitParam(value = "收获人",name = "name")
    })
    @RequestMapping(value = "/updateAddress",method = RequestMethod.POST)
    public  String updateAddress(int addressId,String Provinces,String DetailedAddress,String ContactNumber,String name){
        Address address=new Address();
        address.setProvinces(Provinces);
        address.setName(name);
        address.setDetailedaddress(DetailedAddress);
        address.setContactnumber(ContactNumber);
        address.setAddressid(addressId);
        int i=addressService.updateByAddress(address);
        if (i>0){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }

    //查看收货地址
    @ApiOperation(value = "查看收货地址",notes = "查看收货地址")
    @ApiImplicitParam(value = "用户id",name = "userId")
    @RequestMapping(value = "/selectAddressByUserId",method = RequestMethod.POST)
    public String selectAddressByUserId(int userId){
        List<Address> addressList=addressService.selectByUserId(userId);
        return JSONArray.toJSONString(addressList);
    }

    //修改默认收货地址
    @ApiOperation(value = "修改默认收货地址",notes = "修改默认收货地址")
    @ApiImplicitParam(value = "收货地址id",name = "addressId")
    @RequestMapping(value ="/updatedefault",method = RequestMethod.POST)
    public String updatedefault(int addressId){
        List<Address> addressList=addressService.selectByisdefault(1);
        for(Iterator<Address> iterator=addressList.iterator();iterator.hasNext();){
            Address address1=iterator.next();
            address1.setIsdefault(0);
            addressService.updateByAddress(address1);
        }
        Address address=new Address();
        address.setAddressid(addressId);
        address.setIsdefault(1);
        int i=addressService.updateByAddress(address);
        if (i>0){
            return  "修改默认地址成功";
        }
        return "修改默认地址失败";
    }
}
